package com.dataaccess.dataaccess.web.service;


import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.model.AlarmRecord;
import com.dataaccess.dataaccess.support.mybatisplus.QueryWrapper;
import com.dataaccess.dataaccess.support.utils.DateUtils;
import com.dataaccess.dataaccess.web.mapper.AlarmRecordMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: YangYu
 * @ClassName: AutoNetworkAlarmDeviceDataChangedService
 * @Date: Created in 10:14 2021/1/21
 * @Description: 水泵服务
 */
@Service
public class WaterPumpAsyncService extends BaseService {

    @Resource
    AlarmRecordMapper alarmRecordMapper;

    @Async
    public void sendAlarm(String number, String time, String alarmTypeName, String content) {
        JSONObject jsonObjectN = new JSONObject();
        jsonObjectN.put("deviceClassify", "10");
        jsonObjectN.put("number", number);
        jsonObjectN.put("interfaceType", "1");
        jsonObjectN.put("alarmtime", time);
        jsonObjectN.put("time", time);
        //jsonObjectN.put("paraminfo", jsonArray);
        jsonObjectN.put("alarmTypeName", alarmTypeName);
        jsonObjectN.put("content", content);
        fireFightingiot(jsonObjectN, "communication", "notificationAlarmHandel", "alarmInfo");
    }

    @Async
    public void sendOpenAlarm(String number, String time, String alarmTypeName, String content) throws Exception {
        //查询这个设备号这种类型的报警上次报警时间，如果时间间隔在1.5小时以内，不报警
        List<AlarmRecord> alarmList = alarmRecordMapper.selectList(new QueryWrapper<AlarmRecord>().eq("number", number).eq("type", alarmTypeName));
        boolean alarmFlag = false;
        if (alarmList.isEmpty()) {//没有记录时添加
            AlarmRecord alarmRecord = new AlarmRecord();
            alarmRecord.setNumber(number);
            alarmRecord.setTime(DateUtil.now());
            alarmRecord.setType(alarmTypeName);
            alarmRecordMapper.insert(alarmRecord);
            alarmFlag = true;
        } else {//有记录时判断加修改
            AlarmRecord alarmRecord = alarmList.get(0);
            String times = alarmRecord.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            boolean timeFlag = DateUtils.judgmentDate(times,6);;
            if (timeFlag) {
                alarmRecord.setTime(DateUtil.now());
                alarmRecordMapper.update(alarmRecord, new QueryWrapper<AlarmRecord>().eq("number", number).eq("type", alarmTypeName));
                alarmFlag = true;
            }
        }
        if (alarmFlag) {
            JSONObject jsonObjectN = new JSONObject();
            jsonObjectN.put("deviceClassify", "1");
            jsonObjectN.put("number", number);
            jsonObjectN.put("interfaceType", "1");
            jsonObjectN.put("alarmStr", "3101;;1010;;101");
            jsonObjectN.put("alarmtime", time);
            jsonObjectN.put("alarmTypeName", alarmTypeName);
            jsonObjectN.put("content", content);
            fireFightingiot(jsonObjectN, "communication", "notificationAlarmHandel", "alarmInfo");
        }
    }

    @Async
    public void updateOpeningState(String number, String openingState) {
        JSONObject jsonObjectN = new JSONObject();
        jsonObjectN.put("number", number);
        jsonObjectN.put("openingState", openingState);
        fireFightingiot(jsonObjectN, "firefightingiot", "updateOpeningState", "device");
    }
}
